//
//  CEISliderPlot.m
//  Balance
//
//  Created by Calvin Kuo on 12-05-14.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//


/*
 Status: changed
 Last Merge Date: 05-30-2012
 Last Change Date: 
 Changes:
 1. correct the init value for fromIndex and toIndex
 */

#import "CEISliderPlot.h"



@implementation CEISliderPlot

#define oneDayInterval 86400 //60*60*24
#define oneMonthInterval 2592000 //60*60*24*30 
#define oneYearInterval 31536000 //60*60*24*365
#define kSliderPlot @"SliderPlot"  
#define kFromIndicatorPlot @"FromIndicatorPlot" 
#define kToIndicatorPlot @"ToIndicatorPlot"
#define kRangePlot @"RangePlot"
#define numberOfMarkerPlotSymbols 3

@synthesize delegate;




- (id)init
{
    self = [super init];
    if (self) { 
        dailyCEI = nil;
    }
    
    return self;
}


- (id) initWithOneCollectorData:(NSMutableDictionary *) ceiData monthlyData:(NSArray *)monthlyData{
    self = [self init];
    if (self) {
        dailyCEI = ceiData;
        dailyKeys = [dailyCEI allKeys];
        dailyKeys = [dailyKeys sortedArrayUsingSelector:@selector(compare:)];
        //startDateString = [dailyKeys objectAtIndex:0];
        totalNoOfDataPoints = [dailyCEI count];
        monthlyKeys = monthlyData;
        numberOfPeriods = [monthlyKeys count];
        
        
        
       
       // NSDateFormatter *df = [[NSDateFormatter alloc] init];
        //[df setDateFormat:@"yyyyMMdd"];
       // startDate = [df dateFromString:startDateString];
        startDateData = [dailyCEI objectForKey:[monthlyKeys objectAtIndex:0]];
        startDate = startDateData.ceiDate;
        endDateData =  [dailyCEI objectForKey:[monthlyKeys objectAtIndex:(numberOfPeriods-1)]];
               
        //the initial period range is 6 months
        //last period is next year, in order to display range for this year.
        fromIndex = numberOfPeriods -13; //last year
        toIndex = numberOfPeriods -1;  //this year
        previousFromIndex = fromIndex;
        previousToIndex = toIndex;
        //previousFromCoordination = selectedFromCoordination;
        //previousToCoordination = selectedToCoordination;
        //NSLog(@"init fromIndex=%i,toIndex=%i",fromIndex,toIndex);
        
        rangePlotMoved = NO;

    }
   
    return self;
}


- (void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme{
    
    CGRect bounds = layerHostingView.bounds;
    
    // Create the graph and assign the hosting view.
    graph = [[CPTXYGraph alloc] initWithFrame:bounds];
    layerHostingView.hostedGraph = graph;
    layerHostingView.backgroundColor = [UIColor colorWithWhite:0.7f alpha:1.0f];; 
    
    [graph applyTheme:theme];
    
    graph.plotAreaFrame.masksToBorder = NO;
    
    CPTGradient *plotAreaGradient = [CPTGradient gradientWithBeginningColor:[CPTColor colorWithGenericGray:0.9f]endingColor:[CPTColor colorWithGenericGray:0.7f]];
    plotAreaGradient.angle = -90.0;
    graph.plotAreaFrame.plotArea.fill = [CPTFill fillWithGradient:plotAreaGradient]; 
      
    // Add plot space for horizontal bar charts
    graph.paddingLeft = 90.0;
	graph.paddingTop = 0.0; //10.0 before
	graph.paddingRight = 50.0;
	graph.paddingBottom =30.0;
    
    // Setup plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.delegate = self;
    
    
    //plotSpace.xRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(oneMonthInterval*(numberOfPeriods- 1.0))];
    plotSpace.xRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(startDateData.timeIntervalSinceStart) length:CPTDecimalFromFloat(endDateData.timeIntervalSinceStart-startDateData.timeIntervalSinceStart)];

    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(100)];
    
    
    // Setup grid line style
    CPTMutableLineStyle *majorXGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorXGridLineStyle.lineWidth = .5f;
    majorXGridLineStyle.lineColor = [[CPTColor grayColor] colorWithAlphaComponent:0.25f];
    
    // Setup x-Axis.
	CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x = axisSet.xAxis;
    
    x.majorGridLineStyle = majorXGridLineStyle;
    x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); //x axis starting at 0 in y
    x.majorTickLength = 0.0; //overwrite default in EskPlotTheme.m
    
    //setup x axis label to display the first day of a year
    x.majorIntervalLength = CPTDecimalFromFloat(oneMonthInterval*6);
    x.minorTicksPerInterval = 0;
       
    NSDateFormatter *labelFormatter = [[NSDateFormatter alloc] init];
    [labelFormatter setDateFormat:@"yyyy"];
    
    CPTTimeFormatter *timeFormatter =[[CPTTimeFormatter alloc] initWithDateFormatter:labelFormatter];
    //NSDateFormatter *refDateFormatter = [[NSDateFormatter alloc] init];
    //[refDateFormatter setDateFormat:@"yyyyMMdd"];
    //NSString *refDateString = @"20080101";
    //NSDate *refDate = [refDateFormatter dateFromString: refDateString];
    //timeFormatter.referenceDate = refDate;
    
    timeFormatter.referenceDate = startDate;
    x.labelFormatter = timeFormatter;
    
    // Setup y-Axis.
    CPTMutableLineStyle *majorYGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorYGridLineStyle.lineWidth = 0.5f;
    majorYGridLineStyle.dashPattern =  [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0f], [NSNumber numberWithFloat:5.0f], nil];
    majorYGridLineStyle.lineColor = [[CPTColor lightGrayColor] colorWithAlphaComponent:0.25];
    
    
    CPTXYAxis *y = axisSet.yAxis;
    y.majorGridLineStyle = majorYGridLineStyle;
    y.majorIntervalLength = CPTDecimalFromString(@"10");
    y.minorTicksPerInterval = 1;
    y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
    
    NSArray *yExlusionRanges = [NSArray arrayWithObjects:
                                [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(0.0)],
                                nil];
    y.labelExclusionRanges = yExlusionRanges;
    
    y.labelingPolicy = CPTAxisLabelingPolicyNone; //no label
    
    //Overwrite the color defined in EskPlotTheme.m
    CPTMutableLineStyle *majorLineStyle = [CPTMutableLineStyle lineStyle];
    majorLineStyle.lineCap = kCGLineCapRound;
    majorLineStyle.lineColor = [CPTColor colorWithGenericGray:0.7f];
    majorLineStyle.lineWidth = 1.0;
    x.axisLineStyle = majorLineStyle;
    y.axisLineStyle = majorLineStyle;
    
   	sliderPlot = [[CPTScatterPlot alloc] init];
    sliderPlot.identifier = kSliderPlot;
    
    CPTMutableLineStyle *sliderLineStyle = [sliderPlot.dataLineStyle mutableCopy];
	sliderLineStyle.lineWidth = 1.0f;
    sliderLineStyle.lineColor = [CPTColor colorWithGenericGray:0.2f];
    sliderPlot.dataLineStyle = sliderLineStyle;
    sliderPlot.dataSource = self;
    
    
    CPTGradient *sliderAreaGradient = [CPTGradient gradientWithBeginningColor:[CPTColor colorWithGenericGray:0.3f]endingColor:[CPTColor colorWithGenericGray:0.6f]];
    sliderAreaGradient.angle = -90.0;
    CPTFill *sliderAreaGradientFill = [CPTFill fillWithGradient:sliderAreaGradient];
    sliderPlot.areaFill = sliderAreaGradientFill;
    sliderPlot.areaBaseValue = CPTDecimalFromString(@"0");

    [graph addPlot:sliderPlot];
    
    
    
     
    
    //add the range plot
    rangePlot = [[CPTRangePlot alloc] init];
	rangePlot.identifier = kRangePlot;
    rangePlot.barLineStyle = nil; //no bar displayed
    rangePlot.dataSource = self;
    
    CPTColor *rangeColor = [[CPTColor darkGrayColor] colorWithAlphaComponent:0.6];
	CPTFill *rangeAreaFill = [[CPTFill alloc] initWithColor:(id)rangeColor];
    rangePlot.areaFill = rangeAreaFill;
 
	[graph addPlot:rangePlot];
    rangePlot.delegate= self;
    
   
    //create the slider indicator plots, used to select date ranges. 
    selectedCoordination = 2;
    
    fromIndicatorPlot = [[CPTScatterPlot alloc] initWithFrame:CGRectNull];
    fromIndicatorPlot.identifier = kFromIndicatorPlot;
    fromIndicatorPlot.dataSource = self;
    fromIndicatorPlot.delegate = self;
    
    toIndicatorPlot = [[CPTScatterPlot alloc] initWithFrame:CGRectNull];
    toIndicatorPlot.identifier = kToIndicatorPlot;
    toIndicatorPlot.dataSource = self;
    toIndicatorPlot.delegate = self;
    
    [graph addPlot:fromIndicatorPlot];
    [graph addPlot:toIndicatorPlot];
    [self applyIndicatorPlotColor];
    
    fromIndicatorPlotSelected = NO;
    toIndicatorPlotSelected = NO;
    

    
}


// Assign different color to the touchable line symbol.
- (void)applyIndicatorPlotColor
{
    CPTMutableLineStyle *indicatorLineStyle = [CPTMutableLineStyle lineStyle];
    indicatorLineStyle.lineColor = [CPTColor clearColor];
    indicatorLineStyle.lineWidth = 0.5f;
    fromIndicatorPlot.dataLineStyle = indicatorLineStyle;
    toIndicatorPlot.dataLineStyle = indicatorLineStyle;
}


// Highlight the indicator plot when the user holding tap on the line symbol.
- (void)highlightPlotColor:(CPTScatterPlot *)plot
{
    CPTMutableLineStyle *selectedLineStyle = [CPTMutableLineStyle lineStyle];
    selectedLineStyle.lineColor = [CPTColor clearColor];
    selectedLineStyle.lineWidth = 0.5f;
    
    plot.dataLineStyle = selectedLineStyle;

    
}

#pragma mark - CPPlotSpace Delegate Methods
// This implementation of this method will put the line graph in a fix position so it won't be scrollable in either x or y axis

-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate {
    
    if (coordinate == CPTCoordinateY) 
        return ((CPTXYPlotSpace *)space).yRange;
    else   
        //disable horizontal scrolling
        return ((CPTXYPlotSpace *)space).xRange;
    
}



- (BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(id)event atPoint:(CGPoint)point
{
    
    /*CGPoint pointInPlotArea = [graph convertPoint:point toLayer:graph.plotAreaFrame];
    //BOOL handleEvent = YES;
    
    NSDecimal newPoint[2];
    [graph.defaultPlotSpace plotPoint:newPoint forPlotAreaViewPoint:pointInPlotArea];
    NSDecimalRound(&newPoint[0], &newPoint[0], 0, NSRoundPlain);
    
    int x = [[NSDecimalNumber decimalNumberWithDecimal:newPoint[0]] intValue];
    
    //this is to prevent the slider always going foreward before going backward. This is due to
    //the CGPoint where user press is somewhere between two indicators. Therefore, need to check
    // with next point to find out the direction of the movement.
    if (previousXPoint){   
        if (x > previousXPoint) {
            direction = @"forward";
        } else if (x < previousXPoint) {
            direction = @"backward";
        } else {
            direction = @"stay";
        };
    };
    
    
    if ((x > (oneDayInterval*(totalNoOfDataPoints- 1.0))) || ( x < 0)){  
        return NO;
    };
    
    int d = x /oneMonthInterval;
    if ((fromIndicatorPlotSelected) && (!toIndicatorPlotSelected)){
        
        // NSLog(@"fromIndicatorPlotSelected but not toIndicatorPlotSelected");
        if (d < previousFromCoordination){
            selectedFromCoordination = selectedFromCoordination -1;
        };
        if (d > previousFromCoordination){    
            selectedFromCoordination = selectedFromCoordination +1;
        };
        if (selectedFromCoordination < 0){
            selectedFromCoordination = 0;
            return NO;
        };
    };
    
    
    
    if ((!fromIndicatorPlotSelected) && (toIndicatorPlotSelected)){
        // NSLog(@"Not fromIndicatorPlotSelected but toIndicatorPlotSelected");
        if (d < previousToCoordination){
            selectedToCoordination = selectedToCoordination -1;
        };
        if (d > previousToCoordination){    
            selectedToCoordination = selectedToCoordination +1;
        };
        if(selectedToCoordination > (numberOfPeriods-1)){
            selectedToCoordination = numberOfPeriods -1;
            return NO;
        };
    };
    
    
    if ((!fromIndicatorPlotSelected) && (!toIndicatorPlotSelected)){
        
        int k = (previousXPoint > x) ? (previousXPoint-x)/oneMonthInterval : (x-previousXPoint)/oneMonthInterval;
        
        if ((k > 0) && (selectedFromCoordination > 0) && ([direction isEqualToString:@"backward"])){ 
            selectedFromCoordination = selectedFromCoordination -1;
            selectedToCoordination = selectedToCoordination -1;
            rangePlotMoved = YES;
            previousXPoint = x;
            
        };
        
        if ((selectedToCoordination < (numberOfPeriods-1)) && (k > 0) && ([direction isEqualToString:@"forward"])){
            selectedFromCoordination = selectedFromCoordination +1;
            selectedToCoordination = selectedToCoordination +1;
            rangePlotMoved = YES;
            previousXPoint = x;
            
        };
        
        if ((selectedFromCoordination >0) && (selectedToCoordination < numberOfPeriods)){
            
           
            NSString *newFromDateString = [monthlyKeys objectAtIndex:selectedFromCoordination];
            NSString *newToDateString =[monthlyKeys objectAtIndex:selectedToCoordination];
            
            
            if ([delegate respondsToSelector:@selector(sliderPlot:fromDate:toDate:)])
                [delegate sliderPlot:self fromDate: newFromDateString toDate:newToDateString];
            
            
        }
    };
    

    previousFromCoordination = selectedFromCoordination;
    previousToCoordination = selectedToCoordination;
    
    [graph reloadData];
    return YES;*/
    
    CGPoint pointInPlotArea = [graph convertPoint:point toLayer:graph.plotAreaFrame];
    NSDecimal newPoint[2];
    [graph.defaultPlotSpace plotPoint:newPoint forPlotAreaViewPoint:pointInPlotArea];
    NSDecimalRound(&newPoint[0], &newPoint[0], 0, NSRoundPlain);
    int x = [[NSDecimalNumber decimalNumberWithDecimal:newPoint[0]] intValue];
    int diff;
    if (previousXPoint){   
        if (x > previousXPoint) {
            direction = @"right";
            diff = x - previousXPoint;
        } else if (x < previousXPoint) {
            direction = @"left";
            diff = previousXPoint - x;
        } else {
            direction = @"stay";
        };
    } else{
        previousXPoint = x;
        return NO;
    };
    
    //it's minimum 15 days between 2 data points in slider plot. Therefore, no need to do anything if less than that.
    if (diff < oneDayInterval * 15)
        return NO;
    
    int d = diff/(oneDayInterval * 15);    
    if ((fromIndicatorPlotSelected) && (!toIndicatorPlotSelected)){
        
        if ([direction isEqualToString:@"right"]){
            if ((fromIndex +d) > (numberOfPeriods-1)){
                return NO;
            }
            else{
                fromIndex = fromIndex +d;
                previousXPoint = x;
            };
        }
        
        if ([direction isEqualToString:@"left"]){   
            
            if ((fromIndex -d) < 0 ){
                return NO;
            }
            else{
                fromIndex = fromIndex -d;
                previousXPoint = x;
            }
        };
        if (fromIndex > toIndex)
            return NO;
    };
    
    
    
    if ((!fromIndicatorPlotSelected) && (toIndicatorPlotSelected)){
        if ([direction isEqualToString:@"right"]){
            if ((toIndex +d) > (numberOfPeriods-1))
                return NO;
            else{
                toIndex = toIndex +d;
                previousXPoint = x;
            }
        };
        if ([direction isEqualToString:@"left"]){    
            if ((toIndex -d) < 0 )
                return NO;
            else{
                toIndex = toIndex -d;
                previousXPoint = x;
            }
        };
        if (toIndex < fromIndex)
            return NO;
    };
    
    
    if ((!fromIndicatorPlotSelected) && (!toIndicatorPlotSelected)){
        
        if ([direction isEqualToString:@"left"]){ 
            if ((fromIndex -d) < 0 )
                return NO;
            else{
                fromIndex = fromIndex -d;
                toIndex = toIndex -d;
                rangePlotMoved = YES;
                previousXPoint = x;
            }
        };
        
        if ([direction isEqualToString:@"right"]){
            if ((toIndex +d) > (numberOfPeriods-1))
                return NO;
            else{
                fromIndex = fromIndex + d;
                toIndex = toIndex +d;
                rangePlotMoved = YES;
                previousXPoint = x;
            }
        };
    };
    
    //NSLog(@"dragged fromIndex=%i,toIndex=%i",fromIndex,toIndex);
    
    
    [graph reloadData];
    return YES;

    
}



- (BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event 
          atPoint:(CGPoint)point
{
    
    CGPoint pointInPlotArea = [graph convertPoint:point toLayer:graph.plotAreaFrame];
     
     NSDecimal newPoint[2];
     [graph.defaultPlotSpace plotPoint:newPoint forPlotAreaViewPoint:pointInPlotArea];
     NSDecimalRound(&newPoint[0], &newPoint[0], 0, NSRoundPlain);
     int x = [[NSDecimalNumber decimalNumberWithDecimal:newPoint[0]] intValue];
     
     if ((x > ((fromIndex-1) * oneDayInterval * 15)) && (x < ((fromIndex+1) * oneDayInterval * 15) && (!fromIndicatorPlotSelected))){
     fromIndicatorPlotSelected = YES;
     // NSLog(@"FromIndicatorPlotSelected in DownEvent");
     }
     
     if ((x > ((toIndex-1) * oneDayInterval * 15)) && (x < ((toIndex+1) * oneDayInterval * 15) && (!toIndicatorPlotSelected))){
     toIndicatorPlotSelected = YES;
     //NSLog(@"toIndicatorPlotSelected in DownEvent");
     }
     
     
     //NSLog(@"x=%i,fromIndex=%i,toIndex=%i, fromIndexX=%i, toIndexX=%i, fromIndexRange=%i-%i, toIndexRange=%i-%i",x,fromIndex,toIndex,(fromIndex * oneDayInterval * 15),(toIndex * oneDayInterval * 15),  ((fromIndex-1) * oneDayInterval * 15),((fromIndex+1) * oneDayInterval * 15),((toIndex-1) * oneDayInterval * 15),((toIndex+1) * oneDayInterval * 15));
     

    
    return YES;
}

- (BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceUpEvent:(id)event atPoint:(CGPoint)point
{
   
   /* NSString *newFromDateString = [monthlyKeys objectAtIndex:selectedFromCoordination];
    NSString *newToDateString =[monthlyKeys objectAtIndex:selectedToCoordination];
   
    
    [self applyIndicatorPlotColor];
    
    if ((rangePlotMoved == NO) && (selectedFromCoordination >0) && (selectedToCoordination < numberOfPeriods))
    {
       
        if ([delegate respondsToSelector:@selector(sliderPlot:fromDate:toDate:)])
            [delegate sliderPlot:self fromDate: newFromDateString toDate:newToDateString];
        
        
    }
    
    fromIndicatorPlotSelected = NO;
    toIndicatorPlotSelected = NO;
    rangePlotMoved = NO;  
    previousXPoint = 0;
    
    [fromIndicatorPlot reloadData];
    [toIndicatorPlot reloadData];
    return YES;*/
   
    NSString *newFromDateString = [monthlyKeys objectAtIndex:fromIndex];
    NSString *newToDateString =[monthlyKeys objectAtIndex:toIndex];
    
   // NSLog(@"up fromIndex=%i,toIndex=%i",fromIndex,toIndex);
    
    if ((previousFromIndex != fromIndex) || (previousToIndex != toIndex)){
       if ([delegate respondsToSelector:@selector(sliderPlot:fromDate:toDate:)])
            [delegate sliderPlot:self fromDate: newFromDateString toDate:newToDateString];
    };
    
    fromIndicatorPlotSelected = NO;
    toIndicatorPlotSelected = NO;
    rangePlotMoved = NO;  
    previousXPoint = 0;
    previousFromIndex = fromIndex;
    previousToIndex = toIndex;
    
    [fromIndicatorPlot reloadData];
    [toIndicatorPlot reloadData];
    return YES;

}



#pragma mark - 
#pragma mark Scatter plot delegate methods

- (void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
    if (([(NSString *)plot.identifier isEqualToString: kFromIndicatorPlot]) ||
        ([(NSString *)plot.identifier isEqualToString: kToIndicatorPlot]))  
    {   
        fromIndicatorPlotSelected = NO;
        toIndicatorPlotSelected = NO;
        
        if ([(NSString *)plot.identifier isEqualToString: kFromIndicatorPlot]) 
        {
            fromIndicatorPlotSelected = YES;
        };
        if ([(NSString *)plot.identifier isEqualToString: kToIndicatorPlot]) 
        {
            toIndicatorPlotSelected = YES;
        }    
        //NSLog(@" fromIndicatorPlotSelected = %d",  (int)fromIndicatorPlotSelected);
        //NSLog(@" toIndicatorPlotSelected = %d",  (int)toIndicatorPlotSelected);
        
        //[self highlightPlotColor:plot];
        [plot reloadData];
        
    } 
}



#pragma mark -
#pragma mark Plot Data Source Methods

- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{
    if ([(NSString *)plot.identifier isEqualToString:kFromIndicatorPlot])
    {
        return numberOfMarkerPlotSymbols;
    }
    else {
        return numberOfPeriods;
    }
    
    
}

- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{   
     
    NSNumber *num = nil;
    CEIData *indexData = nil;

    if ( [(NSString *)plot.identifier isEqualToString:kSliderPlot]) {   
        
        indexData = [dailyCEI objectForKey: [monthlyKeys objectAtIndex:index]];
        
        if ( fieldEnum == CPTScatterPlotFieldY )
            num= indexData.ceiPercent;
    
        else if (fieldEnum == CPTScatterPlotFieldX)
            num=[NSNumber numberWithFloat: (indexData.timeIntervalSinceStart)];
    }
        
   if (([(NSString *)plot.identifier isEqualToString:kFromIndicatorPlot]) ||
       ([(NSString *)plot.identifier isEqualToString:kToIndicatorPlot])){
       
       if ( fieldEnum == CPTScatterPlotFieldY ){
            switch (index) {
                case 0:
                    num = [NSNumber numberWithInt:0];
                    break;
                case 1:
                    num = [NSNumber numberWithInt:45];
                    break;
                case 2:
                    num = [NSNumber numberWithInt:100];
                    break;    
                default:
                    num = [NSNumber numberWithInt:0];
                    break;
            }
        } 
        else if (fieldEnum == CPTScatterPlotFieldX){
            
            if ([(NSString *)plot.identifier isEqualToString:kFromIndicatorPlot]){
                //num = [NSNumber numberWithInt:oneMonthInterval*(selectedFromCoordination)];
                indexData = [dailyCEI objectForKey: [monthlyKeys objectAtIndex:fromIndex]];
                num = [NSNumber numberWithFloat:indexData.timeIntervalSinceStart];
            } 
            else if ([(NSString *)plot.identifier isEqualToString:kToIndicatorPlot]){
                //num = [NSNumber numberWithInt:oneMonthInterval*(selectedToCoordination)];
                indexData = [dailyCEI objectForKey: [monthlyKeys objectAtIndex:toIndex]];
                num = [NSNumber numberWithFloat:indexData.timeIntervalSinceStart];

            }
            
        };
    };
    
    
    if ( [(NSString *)plot.identifier isEqualToString:kRangePlot] )
    {
        
        switch (fieldEnum) {
            case CPTRangePlotFieldX: //only 2 data points, 0 and 1
                if (index == 0){ 
                    //num = [NSNumber numberWithInt:oneMonthInterval*(selectedFromCoordination)];
                    indexData = [dailyCEI objectForKey: [monthlyKeys objectAtIndex:fromIndex]];
                    num = [NSNumber numberWithFloat:indexData.timeIntervalSinceStart];
                }
                else {
                    //num = [NSNumber numberWithInt:oneMonthInterval*(selectedToCoordination)];
                    indexData = [dailyCEI objectForKey: [monthlyKeys objectAtIndex:toIndex]];
                    num = [NSNumber numberWithFloat:indexData.timeIntervalSinceStart];
                }
                break;
                
            case CPTRangePlotFieldY:
                num = [NSNumber numberWithInt:100];
                break;
                
            case CPTRangePlotFieldHigh:
                num = [NSNumber numberWithInt:99];
                break;
                
            case CPTRangePlotFieldLow:
                num = [NSNumber numberWithInt:100];
                break;
                
            case CPTRangePlotFieldLeft:
                num = [NSNumber numberWithInt:oneMonthInterval*0.5];
                break;
                
            case CPTRangePlotFieldRight:
                num = [NSNumber numberWithInt:oneMonthInterval*0.5];
                break;
                
            default:
                num = [NSNumber numberWithInt:100];
                break;
        }
    };
    
    
    return num;
}

#pragma mark - CPTScatterPlotDataSource Delegate Methods
- (CPTPlotSymbol *) symbolForScatterPlot:(CPTScatterPlot *) plot recordIndex:(NSUInteger)index 
{  
    CPTPlotSymbol *indicatorSymbol = nil; 
 
    if ((plot == fromIndicatorPlot || plot == toIndicatorPlot) && index == 1){
        
        indicatorSymbol = [CPTPlotSymbol rectanglePlotSymbol];
        indicatorSymbol.lineStyle = nil;
        indicatorSymbol.size = CGSizeMake(15.0f,45.0f);
        CPTImage *sliderImage = [CPTImage imageWithCGImage:[UIImage imageNamed:@"slider.png"].CGImage];
        indicatorSymbol.fill = [CPTFill fillWithImage:sliderImage];
        
        
        /*indicatorSymbol.size = CGSizeMake(25.0f,55.0f); 
        CPTImage *leftSliderImage = [CPTImage imageWithCGImage:[UIImage imageNamed:@"slider_left.png"].CGImage];
        CPTImage *rightSliderImage = [CPTImage imageWithCGImage:[UIImage imageNamed:@"slider_right.png"].CGImage];
        
               
        if (plot == fromIndicatorPlot){
            indicatorSymbol.fill = [CPTFill fillWithImage:leftSliderImage];
        };
        
        if (plot == toIndicatorPlot){
            indicatorSymbol.fill = [CPTFill fillWithImage:rightSliderImage];
        };*/
    }
    return indicatorSymbol;   
    
};






@end
